package com.ruoyi.system.service.project.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.project.textbook.Textbook;
import com.ruoyi.system.domain.project.textbook.TextbookTeacher;
import com.ruoyi.system.mapper.project.TextbookMapper;
import com.ruoyi.system.mapper.project.TextbookTeacherMapper;
import com.ruoyi.system.service.project.ITextbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 教材编写Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-13
 */
@Service
public class TextbookServiceImpl implements ITextbookService
{
    @Autowired
    private TextbookMapper textbookMapper;
    @Autowired
    private TextbookTeacherMapper textbookTeacherService;
    /**
     * 查询教材编写
     * 
     * @param id 教材编写主键
     * @return 教材编写
     */
    @Override
    public Textbook selectTextbookById(Long id)
    {
        Textbook textbook = textbookMapper.selectTextbookById(id);
        TextbookTeacher ra = new TextbookTeacher();
        ra.setBookId(id);
        List<TextbookTeacher> teachers = textbookTeacherService.selectTextbookTeacherList(ra);
        textbook.setTextbookTeachers(teachers);
        return textbook;
    }

    /**
     * 查询教材编写列表
     * 
     * @param textbook 教材编写
     * @return 教材编写
     */
    @Override
    public List<Textbook> selectTextbookList(Textbook textbook)
    {
        //如果是老师，则只显示自己上传的
        if(textbook.getCurrentRole()!=null&&textbook.getCurrentRole().equals("teacher")){
            textbook.setUserId( getUserId() );
        }
        //如果是部门，则显示老师是系里的所有
        if(textbook.getCurrentRole()!=null&&textbook.getCurrentRole().equals("department")){
            textbook.setDeptId(SecurityUtils.getDeptId());
        }
        List<Textbook> textbooks = textbookMapper.selectTextbookList(textbook);
        for (Textbook ra : textbooks) {
            TextbookTeacher teacher = new TextbookTeacher();
            teacher.setBookId(ra.getId());
            List<TextbookTeacher> teachers = textbookTeacherService.selectTextbookTeacherList(teacher);
            ra.setTextbookTeachers(teachers);
        }
        return textbooks;
    }

    /**
     * 新增教材编写
     * 
     * @param textbook 教材编写
     * @return 结果
     */
    @Override
    public int insertTextbook(Textbook textbook)
    {
        if(textbook.getId()!=null){
            TextbookTeacher teacher = new TextbookTeacher();
            teacher.setBookId(textbook.getId());
            teacher.setRank( textbook.getRank());
            teacher.setUserId(textbook.getUserId());
            teacher.setUserType(textbook.getUserType());
            teacher.setUserName(textbook.getUserName());
            teacher.setIfCome(textbook.getIfCome());
            teacher.setMyWord(textbook.getMyWord());
            textbookTeacherService.insertTextbookTeacher(teacher);
        }else {
            int result = textbookMapper.insertTextbook(textbook);
            if(result>0){
                TextbookTeacher teacher = new TextbookTeacher();
                teacher.setBookId(textbook.getId());
                teacher.setRank( textbook.getRank());
                teacher.setUserId(textbook.getUserId());
                teacher.setUserType(textbook.getUserType());
                teacher.setUserName(textbook.getUserName());
                teacher.setIfCome(textbook.getIfCome());
                teacher.setMyWord(textbook.getMyWord());
                textbookTeacherService.insertTextbookTeacher(teacher);
            }
        }
        return 1;
    }

    /**
     * 修改教材编写
     * 
     * @param textbook 教材编写
     * @return 结果
     */
    @Override
    public int updateTextbook(Textbook textbook)
    {
        return textbookMapper.updateTextbook(textbook);
    }

    /**
     * 批量删除教材编写
     * 
     * @param ids 需要删除的教材编写主键
     * @return 结果
     */
    @Override
    public int deleteTextbookByIds(Long[] ids)
    {
        for (Long id : ids){
            TextbookTeacher teacher = new TextbookTeacher();
            teacher.setBookId(id);
            List<TextbookTeacher> teachers = textbookTeacherService.selectTextbookTeacherList(teacher);
            if(!teachers.isEmpty()){
                for (TextbookTeacher textbookTeacher : teachers) {
                    textbookTeacherService.deleteTextbookTeacherById(textbookTeacher.getId());
                }
            }
        }
        return textbookMapper.deleteTextbookByIds(ids);
    }

    /**
     * 删除教材编写信息
     * 
     * @param id 教材编写主键
     * @return 结果
     */
    @Override
    public int deleteTextbookById(Long id)
    {
        return textbookMapper.deleteTextbookById(id);
    }
    /**
     * 根据标题查询
     * @param data
     * @return
     */
    @Override
    public Textbook getPaperInfoByTitle(String data) {
        return textbookMapper.getPaperInfoByTitle( data);
    }
}
